define(function (require, exports, module) {

    var uuid = $("#uuid").val();
    if (uuid == null) {
        uuid = '';
    }
//	获取体验券标识数据
    $.ajax({
        url: '/app/experience/initUseable.html?uuid=' + uuid + '&randomTime=' + (new Date()).getTime(),
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $("#canUseExperienceMoney").val(data.experienceAmount.canUseExperienceMoney);
            if (!data.experienceAmount.canUseExperience) {
                $(".choiceExperience").addClass("hide");
                $(".choiceExperience").after("<span>不支持体验券投标</span>");
            } else if (data.experienceCount == 0) {
                $(".choiceExperience").addClass("hide");
                $(".choiceExperience").after("<span>当前没有可用体验券</span>");
            } else if (data.experienceAmount.canUseExperienceMoney == 0) {
                $(".choiceExperience").addClass("hide");
                $(".choiceExperience").after("<span>该标已无体验券可用额度</span>");
            }
        }
    });


//	$choiceExperience = $('.choiceExperience'),//选择体验券按钮
    $checkAllExperience = $('.checkAllExperience input[type="checkbox"]'),				//体验券全选按钮
        $choiceExperienceBtn = $('.choiceExperienceBox ul li'),
        $choiceExperienceInput = $('.choiceExperienceBox ul li input[type="checkbox"]'),	//  单个体验券选择框
//	$choiceExperienceMoney = $('.choiceExperienceMoney'),								//  选中体验券总额
//	$realinvestMoney = $('.realinvestMoney'),											//  实际支付总额
        $experienceMoney = $('.experienceMoney'),											//  单个体验券金额
        $experienceConfirm = $("#experienceConfirm"), 										//  点击确认
        actualPayment = 0,																	//	实际支付总额
        currentCheckedExMoney = 0,  														// 	选中体验券总额     数值型应先赋值为0，表明为数值
        money = $money.val(),																//	输入投资金额
        dataExpChecked = [],
        currentCheckedRedPacket = 0;														//	当前被选中的红包总额


    //***	选择体验券弹窗
    $choiceExperience.click(function () {

        var canUseExperienceMoney = $("#canUseExperienceMoney").val();					//	可用体验券金额,detail.tpl已添加模拟数据
        currentCheckedRedPacket = parseInt($choiceRedPacketMoney.text());				//	当前被选中的红包总额
        $.ajax({
            url: '/app/experience/useable.html?uuid=' + uuid + '&randomTime=' + (new Date()).getTime(),
            type: 'get',
            dataType: 'json',
            cache: false,
            success: function (json) {

                $("#experienceList").html(Handlebars.compile(require("../../../../tpl/invest/ext/experienceList.tpl"))(json));


                require.async(['/plugins/layer-v1.8.4/skin/layer.css'], function () {
                    window.layer.open({
                        type: 1,
                        closeBtn: [0, true],
                        title: "请选择可使用体验券",
                        area: ['300px', 'auto'],
                        border: [1, 1, '#ccc'],
                        shift: 3,
                        content: $('#choiceExperience'),
                        close: function () {

                        },
                        success: function () {
                            var investMoneyVal = $money.val();
                            //  获取隐藏域中的加息券id
                            dataExpChecked = ($("#experienceMoneyIdArry").val()).split(",");

                            //  提示
                            $(".experienceTips").html("").fadeIn();


                            //  第二次进入时，保持上一次选中的状态
                            $.each(dataExpChecked, function (index, value) {
                                $("#choiceExperience" + value).attr("checked", true);
                                $("#choiceExperience" + value).parent().addClass("checkInput");
                            });

                            //	第二次进入时，保持上一次选中的值

                            if ($choiceExperience.find("em").length == 0) {
                                currentCheckedExMoney = 0;
                            } else {
                                currentCheckedExMoney = parseInt($choiceExperience.find("em").text());		//	'弹框'当前选中加息券 等于'页面'当前选中加息券
                            }

                            $realinvestMoney.text(investMoneyVal - currentCheckedRedPacket);		//	实际支付总额 = 输入金额 - 红包金额
                            $choiceExperienceMoney.text(currentCheckedExMoney);					//	当前选中体验券总额

                        }
                    });

                    $experienceConfirm.click(function () {
                        //	点击确认时初始化，防止更改之后，数组累计
                        for (var i = dataExpChecked.length; i > 0; i--) {
                            dataExpChecked.pop();
                        }

                        //	将选中加息券的id追加到dataExpChecked数组
                        $('.choiceExperienceBox ul li input[type="checkbox"]').each(function () {
                            if ($(this).attr("checked")) {
                                dataExpChecked.push($(this).val());
                            }
                        });

                        //	选中id数组赋值给页面隐藏域
                        $("#experienceMoneyIdArry").val(dataExpChecked);

                        layer.closeAll();
                        var money = 0;
                        if ($money.val() == 0) {
                            money = 0;
                        } else {
                            money = parseInt($money.val() || 0);
                        }
                        //	页面显示
                        $finalActualPayment.html("实际支付：<em>" + (money - currentCheckedRedPacket) + "</em>元");
                        $finalInvestMoney.html("实际投资：<em>" + (money + currentCheckedExMoney) + "</em>元");
                        $choiceExperience.html("使用体验券：<em>" + currentCheckedExMoney + "</em>元&nbsp;&nbsp;&nbsp;");
                    });
                    FnCheckExpAll($('.checkAllExperience input[type="checkbox"]'));
                    FnCheckExpSingle($('#experienceList li'));
                });
            }
        });
    });

    function FnCheckExpAll(obj) {
        obj.on("click", function () {
            var
                canUseExperienceMoney = $("#canUseExperienceMoney").val(),							//	可用体验券金额,

//    		$choiceExperience = $('.choiceExperience'),//选择体验券按钮
                $checkAllExperience = $('.checkAllExperience input[type="checkbox"]'),				//体验券全选按钮
                $choiceExperienceBtn = $('.choiceExperienceBox ul li'),
                $choiceExperienceInput = $('.choiceExperienceBox ul li input[type="checkbox"]'),	//单个体验券选择框
//    		$choiceExperienceMoney = $('.choiceExperienceMoney'),								//选中体验券总额
//    		$realinvestMoney = $('.realinvestMoney'),											//实际支付总额
                $experienceMoney = $('.experienceMoney'),											//单个体验券金额
                $experienceConfirm = $("#experienceConfirm"), 										//点击确认
                actualPayment = 0,																	//	实际支付总额
//    	    canUseExperienceMoney = $("#canUseExperienceMoney").val(),							//	可用体验券金额,detail.tpl已添加模拟数据
//    	    canUseExperienceMoney = 200,//	模拟数据

                currentCheckedRedPacket = 0;														//	当前被选中的红包总额

            currentCheckedRedPacket = parseInt($choiceRedPacketMoney.text());	//	当前被选中的红包总额
            var flag = this.checked;
            if ($money.val() != '') {
                money = parseInt($money.val());
                actualPayment = money;
            }
            $choiceExperienceInput.each(function () {
                if (flag == true) {	//	全选
                    if ($(this).attr('checked')) {
                        //  当前已经被选中，则不需要任何操作
                    } else {
                        //  此时未被选中，应标识checked，且加上当前红包金额
                        //  按钮状态切换
                        $(this).parent().addClass('checkInput');
                        $(this).attr('checked', true);

                        //  金额计算
                        currentCheckedExMoney += parseInt($(this).parent().parent().find(".experienceMoney").text());

                        if (currentCheckedExMoney > canUseExperienceMoney) {

                            //	切换状态
                            $(this).parent().removeClass('checkInput');
                            $(this).attr('checked', false);

                            //	金额计算
                            currentCheckedExMoney -= parseInt($(this).parent().parent().find(".experienceMoney").text());

                            //  提示
                            $(".experienceTips").html("").fadeIn();
                            $(".experienceTips").html("体验券使用总额不得超过<em>" + canUseExperienceMoney + "</em>元").delay(1000).fadeOut();

                            return false;
                        }

                    }


                } else if (flag == false) {	//	当前为非全选状态
                    $choiceExperienceInput.each(function () {
                        if ($(this).attr('checked') == false) {

                        }
                        //	切换状态
                        $(this).parent().removeClass('checkInput');
                        $(this).attr('checked', false);

                        currentCheckedExMoney = 0;    // 重新对选中体验券赋值，且为0

                    });
                }
            });
            // 计算实际支付总额
            if ($money.val() == 0) {
                actualPayment = 0;
            } else {
                actualPayment = $money.val() - currentCheckedRedPacket;
            }

            //   弹框显示
            $choiceExperienceMoney.text(currentCheckedExMoney); //	弹框选中体验券总额
            $realinvestMoney.text(actualPayment);  // 弹框实际支付总额

        });
    }

    function FnCheckExpSingle(obj) {
        obj.on("click", function () {
            var canUseExperienceMoney = $("#canUseExperienceMoney").val();
            currentCheckedRedPacket = parseInt($choiceRedPacketMoney.text());	//	当前被选中的红包总额

            if ($money.val() != '') {
                money = parseInt($money.val());
                actualPayment = money - currentCheckedRedPacket;
            }

            $('#checkAllExperience').attr('checked', false);
            var $parent = $(this).find('label');

            if ($parent.hasClass('checkInput')) {

                //	切换状态
                $parent.removeClass('checkInput')
                $parent.children('input').attr('checked', false);

                //	金额计算
                currentCheckedExMoney -= parseInt($(this).find(".experienceMoney").text());


            } else {

                $parent.addClass('checkInput');
                $parent.children('input').attr('checked', true);

                //	金额计算
                currentCheckedExMoney += parseInt($(this).find(".experienceMoney").text());
                if (currentCheckedExMoney > canUseExperienceMoney) {

                    //	切换专题
                    $parent.removeClass('checkInput');
                    $parent.children('input').attr('checked', false);

                    //	金额计算
                    currentCheckedExMoney -= parseInt($(this).find(".experienceMoney").text());

                    //  提示
                    $(".experienceTips").html("").fadeIn();
                    $(".experienceTips").html("体验券使用总额不得超过<em>" + canUseExperienceMoney + "</em>元").delay(1000).fadeOut();


                    return false;
                }
            }

            // 计算实际支付总额
            if ($money.val() == 0) {
                actualPayment = 0;
            } else {
                actualPayment = $money.val() - currentCheckedRedPacket;
            }
            //   弹框显示
            $choiceExperienceMoney.text(currentCheckedExMoney); //	弹框选中红包总额
            $realinvestMoney.text(actualPayment);  // 弹框实际支付总额

        });
    }


});